<scriptlet>

<implements type="Automation" id="dispatcher">
	<property name="PluginEvent">
	          <get/>
        </property>
	<property name="PluginDescription">
	          <get/>
        </property>
      	<method name="MakeUpper"/>
	<method name="MakeLower"/>
	<method name="SortAscending"/>
	<method name="SortDescending"/>
	<method name="ExecFilterCommand"/>
</implements>

<script language="VBS">
Option Explicit

Function get_PluginEvent()
         get_PluginEvent = "EDITOR_SCRIPT"
End Function

Function get_PluginDescription()
         get_PluginDescription = "Basic text functions for the context menu"
End Function

Function IsLangJapanese()
    Dim languageId
    languageId = 1033
    On Error Resume Next
    languageId = CLng(wsh.RegRead(REGKEY_PATH & "\Locale\LanguageId"))
    IsLangJapanese = (languageId = 1041)
End Function

' transformation functions
Function MakeUpper(Text)
	MakeUpper = UCase(Text)
End Function

Function MakeLower(Text)
	MakeLower = LCase(Text)
End Function

Function ExecFilterCommand(Text)
	Dim cmd
	If IsLangJapanese() Then
		cmd = InputBox("現在の選択範囲の文字列をフィルタコマンドで処理した結果で置換します。コマンドを入力してください。")
	Else
		cmd = InputBox("Enter filter command to replace the selection.")
	End If
	If cmd = "" Then
		If IsLangJapanese() Then
			Err.Raise 30001, , "キャンセルされました"
		Else
			Err.Raise 30001, , "Canceled"
		End If
		Exit Function
	End If

	On Error Resume Next

	Dim wsh
	Dim path
	Set wsh = CreateObject("WScript.Shell")
	path = wsh.ExpandEnvironmentStrings("%TEMP%\_winmerge_addin_temp_.txt")

	Dim fso
	Dim ts
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set ts = fso.CreateTextFile(path)
	If ts Is Nothing Then
		Exit Function
	End If
	ts.Write Text
	ts.Close

	Dim exe
	Set exe = wsh.Exec("cmd /c type """ & path & """  | " & cmd & " 2>&1")
	If exe Is Nothing Then
		If IsLangJapanese() Then
			MsgBox "コマンド " & cmd & " の実行に失敗しました:" & Err.Description 
		Else
			MsgBox "Failed to execute the command '" & cmd & "':" & Err.Description 
		End If
		fso.DeleteFile path
		Exit Function
	End If

	ExecFilterCommand = ""
	Do Until exe.StdOut.AtEndOfStream
		ExecFilterCommand = ExecFilterCommand & exe.StdOut.ReadLine & vbCrLf
	Loop

	fso.DeleteFile path

End Function


</script>

<script language="JScript">

function SortAscending(Text) {
	var eol = Text.match(/\r\n|\n|\r/);
	var lines = Text.split(eol);

	if (lines.length == 1) {
		return Text;
	} else if (lines[lines.length - 1] == "") {
		lines.pop();
		return lines.sort().join(eol) + eol;
	} else {
		return lines.sort().join(eol);
	}
}

function SortDescending(Text) {
	var eol = Text.match(/\r\n|\n|\r/);
	var lines = Text.split(eol);

	if (lines.length == 1) {
		return Text;
	} else if (lines[lines.length - 1] == "") {
		lines.pop();
		lines.sort(function(a, b) { return a < b ? 1 : -1; });
		return lines.join(eol) + eol;
	} else {
		return lines.sort(function(a, b) { return a < b ? 1 : -1; }).join(eol);
	}
}

</script>

</scriptlet>
